package com.situ.day9;

import org.junit.Test;

import java.util.Arrays;

public class StringDemo {
    @Test
    public void test1() {
        //"abc"双引号括起来的字符串：字符串常量，他也是一个对象
        String str1 = "abc";
        String str2 = new String("abc");
        String str3 = "abc";

        //==:比较的是变量指向的对象的地址值
        System.out.println(str1 == str2);//false
        System.out.println(str1 == str3);//true
        System.out.println(str2 == str3);//false

        //equals：比较字符串的内容是不是相等
        System.out.println(str1.equals(str2));//true
        System.out.println(str1.equals(str3));//true
        System.out.println(str2.equals(str3));//true
    }

    @Test
    public void test2() {
        String str = "java AndroidA";
        System.out.println(str.length());//13
        char[] chars = str.toCharArray();
        char ch = str.charAt(5);
        System.out.println(ch);//A
        System.out.println(str.indexOf('A'));//5
        System.out.println(str.indexOf('X'));//-1 找不到返回-1
        System.out.println(str.indexOf('A', 6));//12
        System.out.println(str.indexOf("And"));//5
        System.out.println(str.lastIndexOf('A'));//12
    }

    @Test
    public void test3() {
        String str1 = "java";
        String str2 = "Java";
        System.out.println(str1.equals(str2));//false
        System.out.println(str1.equalsIgnoreCase(str2));//true

        String str = "java AndroidA";
        System.out.println(str.contains("A"));//true
        System.out.println(str.startsWith("java"));//true
        System.out.println(str.startsWith("And"));//false
        //offset:偏移
        System.out.println(str.startsWith("And", 5));//true
        System.out.println(str.endsWith("oidA"));//true
        System.out.println(str.isEmpty());//false
        //"":空字符串，也是一个对象   ' '
        System.out.println("".isEmpty());//true
        char ch = ' ';
    }

    @Test
    public void test89() {
        //String str = "java And";
        char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
        String str = new String(array);
        System.out.println(str);//"java And"
        char[] chars = str.toCharArray();
        System.out.println(chars);
    }

    @Test
    public void test189() {
        String str = "java AndroidA";
        String newStr = str.replace('A', 'B');
        System.out.println(str);//java AndroidA
        System.out.println(newStr);//java BndroidB
    }

    @Test
    public void test180() {
        String str = "java AndroidA";
        //subtree  substring
        System.out.println(str.substring(5));//AndroidA
        //beginIndex – the beginning index, inclusive   include
        //endIndex – the ending index, exclusive.       exclude
        //[beginIndex,endIndex)
        System.out.println(str.substring(5, 8));//And Andr
    }

    @Test
    public void test181() {
        String str = "    java AndroidA    ";
        String newStr = str.trim();
        System.out.println(str);//    java AndroidA
        System.out.println(newStr);//java AndroidA
    }

    @Test
    public void test56() {
        String str = "java AndroidA";
        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
    }

    @Test
    public void test88() {
        String str = "Java:Andorid:PHP:Python";
        String[] array = str.split(":");
        //System.out.println(array);
        System.out.println(Arrays.toString(array));
    }


    @Test
    public void test809() {
        String str1 = "Java";
        String str2 = "PHP";
        String str3 = "Python";
        String str4 = "UI";
        String str = str1 + str2 + str3 + str4;
        System.out.println(str);

        StringBuffer buffer = new StringBuffer("C");
//        buffer.append(str1);
//        buffer.append(str2);
//        buffer.append(str3);
//        buffer.append(str4);
        //链式编程
        buffer.append(str1).append(str2).append(str3).append(str4);
        System.out.println(buffer);

        StringBuilder builder = new StringBuilder("C");
//        builder.append(str1);
//        builder.append(str2);
//        builder.append(str3);
//        builder.append(str4);
        builder.append(str1).append(str2).append(str3).append(str4);
        System.out.println(builder);
    }





}
